﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using DomainDriver.DomainModeling.DomainObjects;

namespace DomainDriver.DomainModeling.StorageManagers.EntityFrameworkStorage
{
    public static class BatchChangeSetUtilities
    {
        public static IEnumerable<T> WhereWithLocal<K, T>(this DbSet<T> dbSet, Func<T, bool> queryPredicate)
            where T : class, IKeyedDomainObject<K, T>
        {
            IEnumerable<T> dbWhere = dbSet.Where(queryPredicate);
            IEnumerable<T> localWhere = dbSet.Local.Where(queryPredicate);
            return dbWhere.Union(localWhere, new KeyBasedEqualityComparer<K, T>());
        }

        public static IEnumerable<T> WhereWithLocal<T>(this DbSet<T> dbSet, Func<T, bool> queryPredicate)
            where T : class, IEfStorableObject
        {
            IEnumerable<T> dbWhere = dbSet.Where(queryPredicate);
            IEnumerable<T> localWhere = dbSet.Local.Where(queryPredicate);
            return dbWhere.Union(localWhere, new CombinedKeyBasedEqualityComparer<T>());
        }
    }
}